<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam[0].condition">
                <a-select-option key="CustomerName">客户名称</a-select-option>

                <!-- <a-select-option key="DeliverAddress">发货地址</a-select-option> -->
                <a-select-option key="DocumentNo">单据号</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam[0].keyword" placeholder="关键字" @keydown.enter="getDataList" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <template slot="DeliverStatus" slot-scope="text, record, index">
        <a-tag color="blue" v-if="!text"> 未发货</a-tag>
        <a-tag color="blue" v-if="text == 1"> 未发货 </a-tag>
        <a-tag color="green" v-if="text == 2"> 已发货 </a-tag>
      </template>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="!record.DeliverStatus" @click="Review(record.Id)">发货</a>
          <a-divider v-if="!record.DeliverStatus" type="vertical" />
          <a v-if="record.ApprovalStatus" @click="Check(record.Id)">查看</a>
        </template>
      </span>
    </a-table>
    <Delivery :parentObj="this" ref="Delivery" />
    <Approval :parentObj="this" ref="Approval" />
    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import Delivery from '../MySample/Delivery.vue'
import Approval from './Approval.vue'

import EditForm from './EditForm'
import moment from 'moment'
const columns = [
  { title: '客户名称', dataIndex: 'CustomerName', width: '20%' },
  { title: '单据号', dataIndex: 'DocumentNo' },
  {
    title: '申请日期',
    dataIndex: 'CreateTime',

    align: 'center',
    scopedSlots: { customRender: 'CreateTime' },
  },
  { title: '经办人', dataIndex: 'Transactor' },
  // { title: '组织机构', dataIndex: 'DepartmentId' },
  // { title: '经办人', dataIndex: 'Transactor' },
  // { title: '发货状态', dataIndex: 'DeliverStatus' },
  // { title: '快递单号', dataIndex: 'TrackingNumber' },
  { title: '收货地址', dataIndex: 'ReceiveAddress', width: '20%' },

  {
    title: '发货状态',
    dataIndex: 'DeliverStatus',
    align: 'center',
    scopedSlots: { customRender: 'DeliverStatus' },
  },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } },
]

export default {
  props: {
    parentObj: Object,
  },
  components: {
    EditForm,
    Delivery,
    Approval,
  },
  mounted() {
    this.getUserList()

  },
  data() {
    return {
      disabled: false,
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      sorter: { field: 'CreateTime', order: 'desc' },
      loading: false,
      columns,
      queryParam: [{ condition: 'CustomerName' }, { condition: 'ApprovalStatus', keyword: 1 }],
      selectedRowKeys: [],
    }
  },
  methods: {
    getUserList() {
      this.selectedRowKeys = []

      this.$http.post('/Base_Manage/Base_User/GetDataList', {}).then((resJson) => {
        this.List = resJson.Data
        //console.log(this.List)
           this.getDataList()
      })
    },
    Shipments() {
      this.$refs.Delivery.openForm(null, '发货')
    },
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.ApprovalStatus == 1,
        },
      }
    },
    repossess() {
      this.queryParam = [{ condition: 'CustomerName' }, { condition: 'ApprovalStatus', keyword: 1 }]
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/SampleManagement/SPL_SampleApplyMain/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          this.loading = false
          this.data = resJson.Data
          this.data.forEach((ele) => {
            var id = this.List.findIndex((item) => {
              return item.Id == ele.Transactor
            })

            if (id !== -1) {
              ele.Transactor = this.List[id].RealName
            }
          })
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    handleAdd() {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建样品申请信息')
    },

    handleRevise(id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '修改')
    },
    handleEdit(id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑样品申请信息')
    },
    Review(id) {
      this.disabled = true
      this.$refs.Delivery.openForm(id, '发货')
    },
    Check(id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },

    handleDelete(ids) {
      var thisObj = this
      // this.$confirm({
      //   title: '确认删除吗?',
      //   onOk() {
      //     return new Promise((resolve, reject) => {
      //       thisObj.$http.post('/SampleManagement/SPL_SampleApplyMain/DeleteDataOfAll', ids).then((resJson) => {
      //         resolve()

      //         if (resJson.Success) {
      //           thisObj.$message.success('操作成功!')

      //           thisObj.getDataList()
      //         } else {
      //           thisObj.$message.error(resJson.Msg)
      //         }
      //       })
      //     })
      //   },
      // })
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        thisObj.$http
          .post('/SampleManagement/SPL_SampleApplyMain/DeleteDataOfAll', ids)
          .then(resJson => {
            // resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD')
    },
  },
}
</script>
